<?php defined('SYSPATH') or die('No direct script access.');

class Model_Product_Mixmarket extends ORM
{  
    protected $_table_name = 'mixmarket_products'; 
    protected $_reload_on_wakeup = false;

    public function add_product($id, $shop_id, $category_id, $url, $name, $picture, $price,
                $currency, $type_prefix, $vendor_id, $vendor, $model, $description)
    {
        $this->id           = $id;
        $this->shop_id      = $shop_id;
        $this->mixmarket_category_id = $category_id;
        $this->url          = $url;
        $this->name         = $name;
        if (!$name) {
            $this->name = $vendor.' '.$model;    
        }
        $this->picture      = $picture;
        $this->price        = $price;
        $this->currency     = $currency;
        $this->type_prefix  = $type_prefix;
        $this->vendor_id    = $vendor_id;
        $this->vendor       = $vendor;
        $this->model        = $model;
        $this->description  = $description;
        $this->save();
        
        return $this;  
    } 
    
    public function get_products($model)
    {
        $model_id = $model;
        if (is_object($model)) {
            $model_id = $model->id;
        }
        
        if ($products = Cache::instance()->get('mixmarket_products_'.$model_id)) {
            return $products;
        }
        
        $query = ORM::factory('product_mixmarket')
                    ->where('model_id', '=', $model_id)
                    ->order_by('price', 'ASC');
        
        $products = $query->find_all(); 
        Cache::instance()->set('mixmarket_products_'.$model_id, Model_Main::get_array($products));
        return $products; 
    }                  
}